package controller

import (
	"../service"
	"../utils"
	"log"
	"net/http"
	"strconv"
)

var contactService *service.ContactService

// 添加好友
func AddFriend(writer http.ResponseWriter, request *http.Request) {

	err := request.ParseForm()
	if err != nil {
		log.Fatal(err.Error())
	}

	// 获取参数
	strOwnerId := request.Form.Get("ownerId")
	strDstId := request.Form.Get("dstId")

	ownerId, _ := strconv.ParseInt(strOwnerId, 10, 64)
	dstId, _ := strconv.ParseInt(strDstId, 10, 64)
	err = contactService.AddFriend(ownerId, dstId)
	if err != nil {
		utils.WriteResult(writer, -1002, nil, err.Error())
	} else {
		utils.WriteResult(writer, 0, nil, "")
	}
}

/**
 * 获取好友列表
 */
func GetFriendList(writer http.ResponseWriter, request *http.Request) {

	err := request.ParseForm()
	if err != nil {
		log.Fatal(err.Error())
	}

	strOwnerId := request.Form.Get("ownerId")
	ownerId, _ := strconv.ParseInt(strOwnerId, 10, 64)
	friendList, err := contactService.GetFriendList(ownerId)
	if err != nil {
		utils.WriteResult(writer, -1003, nil, err.Error())
	} else {
		utils.WriteResult(writer, 0, friendList, "")
	}
}
